package g4p.tests;

import g4p_controls.GWinApplet;
import g4p_controls.GWinData;
import g4p_controls.GWindow;
import processing.core.PApplet;

// Demonstration of how windows can share data and changes in one window are reflected
// in theother windows.

public class LinkedWindows extends PApplet {

	private GWindow[] window;

	public void setup(){
		size(200,200);
		SharedData.cq = new CircleQ(100,100,60,color(128,0,128));
		createWindows();
	}

	public void createWindows(){
		int col;
		window = new GWindow[3];
		for(int i = 0; i < 3; i++){
			col = (200 << (i * 8)) | 0xff000000;
			window[i] = new GWindow(this, "Window "+i, 220+i*220, 60+i*100, 200, 200, false, JAVA2D);
			window[i].setBackground(col);
			window[i].addDrawHandler(this, "windowDraw"+i);
		}
	}


	/**
	 * Draw for the main window
	 */
	public void draw(){
		pushMatrix();
		background(192);
		fill(0);
		text("Window 0", 20,20);
		if(SharedData.cq != null){
			fill(SharedData.cq.col);
			ellipseMode(CENTER);
			translate(SharedData.cq.x, SharedData.cq.y);
			ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
		}
		popMatrix();
	}

	// Change position of circle in main sketch
	public void keyTyped(){
		switch(key){
		case 'w':
			SharedData.cq.y -= 4;
			break;
		case 's':
			SharedData.cq.y += 4;
			break;
		case 'a':
			SharedData.cq.x -= 4;
			break;
		case 'd':
			SharedData.cq.x += 4;
			break;
		case 'z':
			SharedData.cq.r = max(SharedData.cq.r - 4, 40);
			break;
		case 'x':
			SharedData.cq.r = min(SharedData.cq.r + 4, 80);
			break;
		}
	}

	public void windowDraw0(GWinApplet appc, GWinData data){
		appc.pushMatrix();
		appc.fill(255);
		appc.text("Window 0", 20,20);
		if(SharedData.cq != null){
			appc.fill(SharedData.cq.col);
			appc.ellipseMode(CENTER);
			appc.translate(SharedData.cq.x, SharedData.cq.y);
			appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
		}
		appc.popMatrix();
	}	


	public void windowDraw1(GWinApplet appc, GWinData data){
		appc.pushMatrix();
		appc.fill(0);
		appc.text("Window 1", 20,20);
		if(SharedData.cq != null){
			appc.fill(SharedData.cq.col);
			appc.ellipseMode(CENTER);
			appc.translate(SharedData.cq.x, SharedData.cq.y);
			appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
		}
		appc.popMatrix();
	}	


	public void windowDraw2(GWinApplet appc, GWinData data){
		appc.pushMatrix();
		appc.fill(255,255,0);
		appc.text("Window 2", 20,20);
		if(SharedData.cq != null){
			appc.fill(SharedData.cq.col);
			appc.ellipseMode(CENTER);
			appc.translate(SharedData.cq.x, SharedData.cq.y);
			appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
		}
		appc.popMatrix();
	}	


}
